package edu.neu.cs6515.test.runner;

import java.util.List;

import javax.swing.JFrame;

import edu.neu.cs6515.board.HexagonBoard;
import edu.neu.cs6515.gui.PlayerRenderer;
import edu.neu.cs6515.hci.HCIPanel;
import edu.neu.cs6515.hci.HCIStrategy;
import edu.neu.cs6515.player.strategy.StrategyException;
import edu.neu.cs6515.xml.Placement;

/**
 * class that launches the PlayerGUI
 */
public class PlayerGUITestRunner extends AbstractTestRunner {

    private edu.neu.cs6515.xml.Player player;

    /**
     * C'tor of PlayerGUITestRunner
     * 
     * @param player
     *            xml representation of the player
     */
    public PlayerGUITestRunner(final edu.neu.cs6515.xml.Player player) {
        super(player.getBoard());
        this.player = player;
    }

    @Override
    protected void initializePlacements(final List<Placement> placements) {
        board.updateHexagonBoard(placements);
    }

    /**
     * launch the GUI
     */
    public void runAndShowGUI() {
        JFrame frame = new JFrame("Player Renderer");
        PlayerRenderer playerRenderer = new PlayerRenderer();
        HexagonBoard board = new HexagonBoard(player.getBoard().getPlayers());
        board.updateHexagonBoard(player.getBoard().getPlacement());
        playerRenderer.update(board, player.getScore(), player.getTile());
        frame.getContentPane().add(playerRenderer);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setVisible(true);
    }

    /**
     * launch the HCI
     * 
     * @return the result of HCI
     */
    public String runAndShowHCI() {
        HCIPanel frame = new HCIPanel(60);
        HCIStrategy hciStrategy = new HCIStrategy(player.getInterval(), frame);
        try {
            Placement placement = hciStrategy.getNextMove(player.getTile(),
                    player.getBoard().getPlayers(), player.getBoard()
                            .getPlacement(), player.getScore());
            return placement.toXMLString();
        } catch (StrategyException e) {
            return "<timeout />";
        }
    }
}
